package com.huffman.smart_picture.controller;

import com.huffman.smart_picture.common.auth.AuthConstant;
import com.huffman.smart_picture.common.auth.SessionContext;
import com.huffman.smart_picture.common.auth.annotation.LoginRequired;
import com.huffman.smart_picture.common.EmailService;
import com.huffman.smart_picture.common.UserSession;
import com.huffman.smart_picture.common.base.reponse.ResponseResult;
import com.huffman.smart_picture.common.base.request.SendEmailRequest;
import com.huffman.smart_picture.common.exception.ErrorEnum;
import com.huffman.smart_picture.domain.constants.CommonConstant;
import com.huffman.smart_picture.domain.entity.User;
import com.huffman.smart_picture.domain.enums.PlatformEnum;
import com.huffman.smart_picture.domain.request.user.UserLoginRequest;
import com.huffman.smart_picture.domain.request.user.UserRegisterRequest;
import com.huffman.smart_picture.domain.vo.user.UserVo;
import com.huffman.smart_picture.service.UserService;
import com.huffman.smart_picture.utils.RegexUtils;
import com.huffman.smart_picture.utils.ResponseUtil;
import com.huffman.smart_picture.utils.ThrowUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author DengLin
 * @title 用户认证控制器
 * @date 2024/06/27
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    UserService userService;

    @Resource
    EmailService emailService;

    @Resource
    SessionContext sessionContext;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResponseResult userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        // 数据校验
        ThrowUtil.throwIf(userRegisterRequest == null, ErrorEnum.PARAM_EMPTY, "请求参数为空");
        ThrowUtil.throwIf(!StringUtils.hasText(userRegisterRequest.getUsername()), ErrorEnum.PARAM_EMPTY, "账号为空");
        ThrowUtil.throwIf(!StringUtils.hasText(userRegisterRequest.getPassword()), ErrorEnum.PARAM_EMPTY, "密码为空");
        ThrowUtil.throwIf(!StringUtils.hasText(userRegisterRequest.getCheckPassword()), ErrorEnum.PARAM_EMPTY, "确认密码为空");
        userService.register(userRegisterRequest);
        return ResponseUtil.success();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ResponseResult userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest httpServletRequest) throws InterruptedException {
        // 延迟一秒 增加动画效果
        Thread.sleep(1000);
        // 数据校验
        ThrowUtil.throwIf(userLoginRequest == null, ErrorEnum.PARAM_EMPTY, "请求参数为空");
        String username = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        Integer platform = userLoginRequest.getPlatform();
        ThrowUtil.throwIf(!StringUtils.hasText(username), ErrorEnum.PARAM_EMPTY, "账号为空");
        ThrowUtil.throwIf(!StringUtils.hasText(password), ErrorEnum.PARAM_EMPTY, "密码为空");
        ThrowUtil.throwIf(PlatformEnum.getUserPlatformEnumByCode(platform) == null, ErrorEnum.PARAM_ILLICIT, "平台有误");

        UserVo userVo = userService.login(userLoginRequest);
        UserSession userSession = UserSession.convert(userVo);
        httpServletRequest.getSession().setAttribute(AuthConstant.LOGIN_USER, userSession);
        return ResponseUtil.success(userVo);
    }

    @ApiOperation("用户注销")
    @PostMapping("/logout")
    public ResponseResult userLogout(HttpServletRequest httpServletRequest) {
        HttpSession session = httpServletRequest.getSession();
        session.removeAttribute(AuthConstant.LOGIN_USER);
        return ResponseUtil.success(null, "退出成功", "");
    }

    @ApiOperation("当前用户信息")
    @GetMapping("/info")
    @LoginRequired
    public ResponseResult loginUserInfo() {
        UserSession userSession = sessionContext.getUserSession();
        return ResponseUtil.success(userSession);
    }


    @PostMapping("/email")
    @ApiOperation("邮件发送")
    public ResponseResult sendEmail(@RequestBody SendEmailRequest sendEmailRequest) {
        String email = sendEmailRequest.getEmail();
        ThrowUtil.throwIf(sendEmailRequest == null, ErrorEnum.PARAM_EMPTY, "请求参数为空");
        ThrowUtil.throwIf(!StringUtils.hasText(email), ErrorEnum.PARAM_EMPTY, "邮箱为空");
        boolean exists = userService.lambdaQuery().eq(User::getEmail, email).exists();
        ThrowUtil.throwIf(exists, ErrorEnum.PARAM_ILLICIT, "当前邮箱已注册");
        ThrowUtil.throwIf(!RegexUtils.matches(RegexUtils.REGEX_EMAIL, email), ErrorEnum.PARAM_ILLICIT, "邮箱格式错误");
        emailService.sendEmail(sendEmailRequest);
        return ResponseUtil.success();
    }

}
